const QQ_MAP = require("../libs/qqmap-wx-jssdk.min.js");
import Config from "../main.config";
import CustomError from "../utils/Error";
module.exports = {

    /*解析定位信息*/
    reverseGeocoder: function (location) {
        const qqMapSdk = new QQ_MAP({key: Config.mapKey});
        return new Promise(function (resolve, reject) {
            qqMapSdk.reverseGeocoder({
                location: {latitude: location.latitude, longitude: location.longitude},
                success: function(detailRes) {
                    let landmark = detailRes.result.address_reference.landmark_l1 || detailRes.result.address_reference.landmark_l2
                    let locationDetail = {
                        address: detailRes.result.address,
                        location: detailRes.result.location,
                        province: detailRes.result.address_component.province,
                        city: detailRes.result.address_component.city,
                        district: detailRes.result.address_component.district,
                        town: detailRes.result.address_reference.town.title,
                        street: detailRes.result.address_component.street,
                        region: detailRes.result.address_component.province + ","
                        + detailRes.result.address_component.city + ","
                        + detailRes.result.address_component.district,
                        landmark: landmark.title
                    }
                    resolve(locationDetail);
                },
                fail: function(err) {
                    reject(new CustomError(err, "Location.reverseGeocoder").parse())
                }
            });
        })
    },

    /*
    * 地址解析
    * */
    geocoder: function (addressDetail) {
        const qqMapSdk = new QQ_MAP({key: Config.mapKey});
        return new Promise(function (resolve, reject) {
            qqMapSdk.geocoder({
                //获取表单传入地址
                address: addressDetail, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
                success: function(res) {//成功后的回调
                    console.log(res);
                    resolve({
                        latitude: res.result.location.lat,
                        longitude: res.result.location.lng
                    })
                },
                fail: function(error) {
                    reject(error)
                }
            })
        })
    },

    /*
    * 获取城市列表
    * */
    getCityList: function () {
        const qqMapSdk = new QQ_MAP({key: Config.mapKey});
        return new Promise(function (resolve, reject) {
            qqMapSdk.getCityList({
                success: function (res) {
                    let newArr = [];
                    res.result[0].forEach(function (item) {
                        if (item.fullname.substring(item.fullname.length - 1) === "市") {
                            let hasIndex =  newArr.findIndex(function (x) {
                                return item.pinyin[0].substring(0,1).toUpperCase() === x.first
                            });
                            if (hasIndex >= 0) { //newArr中存在同一个字母的数据
                                newArr[hasIndex].list.push({
                                    id: item.id,
                                    fullName: item.fullname,
                                    pinyin: item.pinyin,
                                    location: item.location,
                                    shorthand: item.pinyin.map(function (x) {
                                        return x.substring(0,1)
                                    }).join("")
                                })
                            } else { //不存在
                                newArr.push({
                                    first: item.pinyin[0].substring(0,1).toUpperCase(),
                                    list: [{
                                        id: item.id,
                                        fullName: item.fullname,
                                        pinyin: item.pinyin,
                                        location: item.location,
                                        shorthand: item.pinyin.map(function (x) {
                                            return x.substring(0,1)
                                        }).join("")
                                    }]
                                })
                            }
                        }
                    });
                    res.result[1].forEach(function (item) {
                        if (item.fullname.substring(item.fullname.length - 1) !== "区") {
                            let hasIndex =  newArr.findIndex(function (x) {
                                return item.pinyin[0].substring(0,1).toUpperCase() === x.first
                            });
                            if (hasIndex >= 0) { //newArr中存在同一个字母的数据
                                newArr[hasIndex].list.push({
                                    id: item.id,
                                    fullName: item.fullname,
                                    pinyin: item.pinyin,
                                    location: item.location,
                                    shorthand: item.pinyin.map(function (x) {
                                        return x.substring(0,1)
                                    }).join("")
                                })
                            } else { //不存在
                                newArr.push({
                                    first: item.pinyin[0].substring(0,1).toUpperCase(),
                                    list: [{
                                        id: item.id,
                                        fullName: item.fullname,
                                        pinyin: item.pinyin,
                                        location: item.location,
                                        shorthand: item.pinyin.map(function (x) {
                                            return x.substring(0,1)
                                        }).join("")
                                    }]
                                })
                            }
                        }
                    });
                    newArr.sort(function (x, y) {
                        let a = x.first.toLowerCase();
                        let b = y.first.toLowerCase();
                        if (a < b) return -1;
                        if (a > b) return 1;
                        return 0;
                    });
                    resolve(newArr)
                },
                fail: function (err) {
                    reject(new CustomError(err, "Location.getCityList").parse())
                }
            });
        })
    },

    /*
    * 获取区域数据
    * */
    getRegions: async function () {
        const qqMapSdk = new QQ_MAP({key: Config.mapKey});
        return new Promise(function (resolve, reject) {
            qqMapSdk.getCityList({
                success: function (res) {
                    resolve(res.result)
                },
                fail: function (err) {
                    reject(new CustomError(err, "Location.getRegions").parse())
                }
            })
        })
    }
}